package conteoller;

import biz.ColorBiz;
import com.google.gson.Gson;
import entity.ColortbEntity;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/*
 * @Description：
 *       处理颜色数据
 * @Author: JiangFan
 * @Date: 2019/3/8
 * @Time：17:04
 */
@WebServlet("/color")
@MultipartConfig
public class ColorServlet extends BaseServlet{
    ColorBiz cb=new ColorBiz ();
    ColortbEntity ce=new ColortbEntity ();

    /** 查询所有颜色
     * @param req
     * @param reps
     * @return
     */
    public String queryColorAll(HttpServletRequest req, HttpServletResponse reps)
            throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        List<ColortbEntity> list=cb.queryColorAll ();
        Gson gson=new Gson ();
        String json=gson.toJson (list);
        return json;
    }

    /** 添加颜色是否存在
     * @param req
     * @param resp
     * @return 返回空值，不影响页面跳转
     */
    public String queryColorOne(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        //取编号
        String color=req.getParameter ("txtColor");
        //调用Biz查询方法,查询颜色是否可用
        try {
            boolean ce=cb.checkColor (color);
            if (ce){
                resp.getWriter ().write ("颜色可用");
            }else {
                resp.getWriter ().write ("颜色已存在");
            }
        }catch (Exception e){
            //响应错误消息文本
            resp.getWriter ().write ("颜色不可用");
        }
        return null;
    }
    /*public String addColor(HttpServletRequest req,HttpServletResponse resp){
        ce.setColor_name (req.getParameter ("txtColor"));
    }*/
}
